/*
 * Copyright (c) 2021, 2022, yzfh.cn All rights reserved.
 *
 */
package com.ly.service.impl;

import com.ly.dao.RoleDao;
import com.ly.dao.UsrDao;
import com.ly.domain.Role;
import com.ly.domain.User;
import com.ly.service.UserService;
import org.springframework.dao.EmptyResultDataAccessException;

import java.util.List;

/**
 * @author yzfh
 * @Date 2022/02/19 星期六 12:48:13
 */
public class UserServiceImpl implements UserService {

    private UsrDao userDao;

    public void setUserDao(UsrDao userDao) {
        this.userDao = userDao;
    }

    private RoleDao roleDao;

    public void setRoleDao(RoleDao roleDao) {
        this.roleDao = roleDao;
    }

    @Override
    public List<User> list() {
        List<User> userList = userDao.findAll();
        //封装userList中每一个User的roles数据
        for (User user :userList){
            //获得user的id
            Long id = user.getId();
            //将id作为参数 查询当前userId对应的Role集合数据
            List<Role> roles = roleDao.findRoleByUserId(id);
            user.setRoles(roles);
        }
        return userList;
    }

    @Override
    public void save(User user, Long[] roleIds) {
        //第一步 向sys_user表中存储数据
        Long userId = userDao.save(user);
        //第二步 向sys_user_role关系表中存储多条数据
        userDao.saveUserRoleRel(userId,roleIds);
    }

    @Override
    public void del(Long userId) {
        //1、删除sys_user_role关系表
        userDao.delUserRoleRel(userId);
        //2、删除sys_user表
        userDao.del(userId);
    }

    @Override
    public User login(String username, String password) {
        try {
            User user = userDao.findByUsernameAndPassword(username, password);
            return user;
        }catch (EmptyResultDataAccessException e) {
            return null;
        }
    }
}
